﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
#if WPF
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Shapes;
using System.Windows.Media;
#else
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI;
#endif


namespace Eternal.DataVisualization
{
    public class BubbleSeries : AxisType
    {
        protected IList<double> BubbleValues;

        public string BubbleSizePath
        {
            get { return (string)GetValue(BubbleSizePathProperty); }
            set { SetValue(BubbleSizePathProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BubbleSizePath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BubbleSizePathProperty =
            DependencyProperty.Register("BubbleSizePath", typeof(string), typeof(BubbleSeries), new PropertyMetadata(string.Empty));

        public double MaximumSize
        {
            get { return (double)GetValue(MaximumSizeProperty); }
            set { SetValue(MaximumSizeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MaximumSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MaximumSizeProperty =
            DependencyProperty.Register("MaximumSize", typeof(double), typeof(BubbleSeries), new PropertyMetadata(50d));

        internal override void GenerateDatas()
        {
            if (ItemsSource != null && !String.IsNullOrEmpty(PrimaryPath))
            {
                var type = "";
                ActualXData = DataIterator.Iterate(ItemsSource, PrimaryPath, ref type);
                XDataType = type;
                ActualYData = DataIterator.IterateDoubleValue(ItemsSource, SecondaryPath, ref type);
                YDataType = type;
                BubbleValues = DataIterator.IterateDoubleValue(ItemsSource, BubbleSizePath, ref type);
                PointsGenerated = true;
                IsRangeUpdateNeeded = true;
            }
        }

        protected internal override void OnDrawSegment()
        {
            base.OnDrawSegment();
            if (XAxis != null && YAxis != null)
            {
                SegmentPanel.Children.Clear();

                var max = BubbleValues.Max();

                var diff = MaximumSize / max;
               
                for (var i = (int)Math.Floor(XAxis.Start); i <= (int)Math.Ceiling(XAxis.End) && BubbleValues.Count > i; i++)
                {
                    var scatterRadius = BubbleValues[i];
                    var ellips = new Ellipse {Height = scatterRadius*2*diff, Width = scatterRadius*2*diff};
                    ApplyBrush(ellips, i);
                    SegmentPanel.Children.Add(ellips);

                    Canvas.SetLeft(ellips, ValueToCoordianteXAxis(i) - ellips.Width / 2);
                    Canvas.SetTop(ellips, ValueToCoordianteYAxis(ActualYData[i]) - ellips.Height / 2);
                }
            }
        }

        Ellipse previousElement;
#if WPF
        protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
#else
        protected override void OnPointerMoved(Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
#endif
        {
            if (previousElement != null)
            {
                var brush = (previousElement.Fill as SolidColorBrush).Color;
                previousElement.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(brush.R / 0.6), (byte)(brush.G / 0.6), (byte)(brush.B / 0.6)));
                previousElement.StrokeThickness = 0;
                previousElement = null;
            }
            var rect = e.OriginalSource as Ellipse;

            if (rect != null)
            {
                var brush = (rect.Fill as SolidColorBrush).Color;
                rect.Fill = new SolidColorBrush(Color.FromArgb(255, (byte)(brush.R * 0.6), (byte)(brush.G * 0.6), (byte)(brush.B * 0.6)));
                previousElement = rect;
            }
#if WPF
            base.OnMouseMove(e);
#else
            base.OnPointerMoved(e);
#endif
        }
    }
}
